package com.tengju.user.infrastructure.repository.convert;

import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.StaffId;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.UserStaffDO;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author wuziyang
 */
public class StaffInfoConvert {

    public static Staff convertStaffDOToStaff(UserStaffDO userStaffDO) {
        if (userStaffDO == null) {
            return null;
        }
        return new Staff(
                new StaffId(userStaffDO.getStaffId()),
                userStaffDO.getName(),
                userStaffDO.getHeadImage(),
                userStaffDO.getCreateBy(),
                userStaffDO.getCreateTime() == null ? null : LocalDateTime
                        .ofInstant(userStaffDO.getCreateTime().toInstant(), ZoneId.systemDefault()),
                userStaffDO.getUpdateBy(),
                userStaffDO.getUpdateTime() == null ? null : LocalDateTime
                        .ofInstant(userStaffDO.getUpdateTime().toInstant(), ZoneId.systemDefault())
        );
    }

    public static UserStaffDO convertStaffToStaffDO(Staff staff) {
        if (staff == null) {
            return null;
        }
        UserStaffDO userStaffDO = new UserStaffDO();
        userStaffDO.setStaffId(staff.getStaffId() == null ? null : staff.getStaffId().getValue());
        userStaffDO.setName(staff.getName());
        userStaffDO.setExtra(null);
        userStaffDO.setCreateBy(staff.getCreateBy());
        userStaffDO.setCreateTime(staff.getCreateTime() == null ? null :
                Date.from(staff.getCreateTime().atZone(ZoneId.systemDefault()).toInstant()));
        userStaffDO.setUpdateBy(staff.getUpdateBy());
        userStaffDO.setUpdateTime(staff.getUpdateTime() == null ? null :
                Date.from(staff.getUpdateTime().atZone(ZoneId.systemDefault()).toInstant()));
        userStaffDO.setIsDelete(null);
        userStaffDO.setHeadImage(staff.getHeadImage());
        return userStaffDO;
    }
}
